<?
#include "DB/DataBase.class.php";
#include "GridSource.class.php";
#include "Grid.class.php";

class GridSorter{
	


	public static function SortQuery($sql, $sort_by){
	
		$has_order_clause = strrpos($sql, "ORDER BY");
		if(!$has_order_clause && $sort_by != "")
			$sql.= " ORDER BY ".$sort_by;
					
		return $sql;
	}

	public static function Render($matrix, $fields_name, $list = array(),  $has_header = true){

		$html = "<table>\n";
					 
		foreach($matrix as $key => $rows){	

			if($has_header && $key ==0) $html .= "<thead>";

			$html .= "<tr>\n";			
			
			foreach($rows as $index => $column){
				if($has_header && $key == 0)
					$html .= "<td><a href=\"".$PHP_SELF."?order=$fields_name[$index]\">".$column."</a></td>";
				else{
					if($key%2 == 0)		$html .= "<td>".$column."</td>";
					else				$html .= "<td class=\"selectedRow\">".$column."</td>";
				}
			}
			
			#-------
			$html .= "<td>";
			if($has_header && $key == 0){
				$html .= "Opciones";
			}else{
				foreach($list as $url)
					$html .= $url->Render($rows)."&nbsp;|&nbsp;";	
			}
			$html .= "</td>";
			#-------
				
			$html .= "\n</tr>\n";
			if($has_header && $key ==0) $html .= "</thead>";
		}
		$html .= "</table>\n";	
		return $html;
	 } #GetHTMLTable
	 
	 
	 public static function GetColumnNames($sql){
	 	return DataBase::FieldsName($sql);
	 }
}

#$sql = "SELECT * FROM socios";

#$cols = GridSorter::GetColumnNames("SELECT * FROM socios");
#$g = new Grid();
#GridDataSource::Fill($g, $sql);
#echo GridSorter::Render($g->grid, DataBase::FieldsName($sql));
#print_r($cols);
?>